﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SeDuce.IDAL;
using System.Data.SqlClient;

namespace SeDuce.DAL
{
    public class BrandDAL : IBrandDAL
    {
        #region SQL
        public const string sqlGetBrands = "SELECT * FROM TbBrand";
        public const string sqlGetBrandByBrandID = "SELECT * FROM TbBrand where BrandID={0}";
        const string sqlUpdateBrand = "UPDATE TbBrand SET BrandName = {0}, BrandImage = {1},Sort = {2} WHERE (BrandID = {3})";
        const string sqlInsertBrand = "INSERT INTO TbBrand (BrandName, BrandImage,Sort) VALUES ({0},{1},{2})";
        const string sqlDeleteBrand = "Delete from TbBrand where BrandID={0}";
        #endregion

        #region Variables
        private static DataAccessObjectBase _dao = null;
        public static DataAccessObjectBase DAO
        {
            get
            {
                if (_dao == null)
                    _dao = new DataAccessObjectBase();
                return _dao;
            }
            set
            {
                _dao = value;
            }
        }
        #endregion

        public System.Data.DataTable GetBrands()
        {
            return DAO.Select(sqlGetBrands);
        }

        public System.Data.DataTable GetBrandByBrandID(int brandID)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@BrandID", brandID)
            };
            string sql = string.Format(sqlGetBrandByBrandID, "@BrandID");
            return DAO.Select(sql, sp);
        }

        public bool UpdateBrand(SeDuce.Model.Brand brand)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@BrandName", brand.BrandName),
                new SqlParameter ("@BrandImage", brand.BrandImage),
                new SqlParameter ("@Sort", brand.Sort),
                new SqlParameter ("@BrandID", brand.BrandID)
            };
            string sql = string.Format(sqlUpdateBrand, "@BrandName", "@BrandImage", "@Sort", "@BrandID");
            return DAO.ExecuteSql(sql, sp);
        }

        public bool UpdateBrands(List<Model.Brand> brands)
        {
            Dictionary<SqlParameter[], string> updateSqls = new Dictionary<SqlParameter[], string>();
            for (int i = 0; i < brands.Count; i++)
            {
                SqlParameter[] parms = new SqlParameter[] { 
                    new SqlParameter ("@BrandName",brands[i].BrandName),
                    new SqlParameter ("@BrandImage",brands[i].BrandImage),
                    new SqlParameter ("@Sort",brands[i].Sort),
                    new SqlParameter ("@BrandID",brands[i].BrandID)
                  };
                string sql = string.Format(sqlUpdateBrand, "@BrandName", "@BrandImage", "@Sort", "@BrandID");
                updateSqls.Add(parms, sql);
            }
            return DAO.ExecuteSqlTran(updateSqls);
        }

        public bool InsertBrand(SeDuce.Model.Brand brand)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@BrandName", brand.BrandName),
                new SqlParameter ("@BrandImage", brand.BrandImage),
                new SqlParameter ("@Sort", brand.Sort)
            };
            string sql = string.Format(sqlInsertBrand, "@BrandName", "@BrandImage", "@Sort");

            return DAO.ExecuteSql(sql, sp);
        }

        public bool DeleteBrand(int brandID)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter ("@BrandID",brandID),
    
            };
            string sql = string.Format(sqlDeleteBrand, "@BrandID");
            return DAO.ExecuteSql(sql, sp);
        }
    }
}
